<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="所有建议"></uni-navbar></view>

		<view class="search-view"><u-search class="search-input" shape="square" bg-color="#fff" v-model="motion.title" @search="search"></u-search></view>

		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
			<view class="list-info_01" v-for="(item, index) of proposalList" :key="item.id" @click="navTo(item)">
				<view class="info-title">{{ item.title }}</view>
				<view class="info-btm">
					<view class="btm-left clamp">
						<text class="left-name">{{ item.leaderName }}</text>
						<text class="left-time">{{ item.createTime | parseTime('{y}-{m}-{d}') }}</text>
					</view>
					<view class="btm-right">
						<view class="right-type" :style="'color:' + colorMap[item.nodeStatus]">{{ item.nodeStatusName }}</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="loading" />
		</view>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			scrollTop: 0,
			show: false,
			tabsCurrent: 0,
			tabsList: [
				{
					name: '所有',
					value: ''
				},
				{
					name: '代表团待审查',
					value: '101'
				},
				{
					name: '代表室待审查',
					value: '1'
				},
				{
					name: '待交办',
					value: '3'
				},
				{
					name: '交办中',
					value: '4'
				},
				{
					name: '办理中',
					value: '5'
				},
				{
					name: '已办结',
					value: '8'
				}
			],
			colorMap: {
				0: '#303133',
				1: '#f29100',
				2: '#18b566',
				3: '#dd6161',
				4: '#0099ff',
				5: '#ff9900',
				6: '#19be6b',
				7: '#ff3333',
				8: '#909399',
				101: '#71d5a1'
			},
			title: '',
			jieci: '',
			jcShow: false,
			jcList: [],

			kind: '',
			kindShow: false,
			kindList: [
				{
					value: '1',
					label: '个人建议'
				},
				{
					value: '2',
					label: '集体建议'
				},
				{
					value: '3',
					label: '联名建议'
				}
			],

			type: '',
			typeShow: false,
			typeList: [],

			wyhdz: '',
			wyhdzShow: false,
			wyhdzList: [],

			dept: '',
			deptShow: false,
			deptList: [],

			deptXb: '',
			deptXbShow: false,
			deptXbList: [],

			startTime: '',
			endTime: '',
			proposalList: [],

			motion: {
				deptId: '',
				wyhdz: '',
				ciId: '',
				jieId: '',
				type: '',
				leader: '',
				leaderName: '',
				dateRangeSea: '',
				nodeStatus: '3,4,5,6,7,8',
				title: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		if (option && option.currentUserId) {
			this.motion.leader = option.currentUserId;
		}
		if (option && option.wyhdz) {
			this.motion.wyhdz = option.wyhdz;
		}
		if (option && option.jieId) {
			this.motion.jieId = option.jieId;
		}
		if (option && option.ciId) {
			this.motion.ciId = option.ciId;
		}
		if (option && option.jieci) {
			this.jieci = option.jieci;
		}
		this.loadData();
		this.jieciData();
		this.loadDept();
		this.loadDict('motion_type', 'typeList');
		this.loadDict('wyhdz', 'wyhdzList');
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		dateRangeSea: {
			get: function() {
				let time = this.startTime + ' - ' + this.endTime;
				if (this.startTime == '' || this.endTime == '') {
					return '';
				} else {
					return time;
				}
			},
			set: function(e) {
				this.startTime = '';
				this.endTime = '';
			}
		},
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;

				this.jieci = this.userInfo.jieCi.pName + this.userInfo.jieCi.name;
				this.motion.jieId = this.userInfo.jieCi.pid;
				this.motion.ciId = this.userInfo.jieCi.id;

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				this.$logout();
			}
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				clientCode: this.clientCode,
				currentUserId: this.userId,
				deptId: this.motion.deptId,
				motion: this.motion
			};
			let url = '/motion/list';
			if (this.motion.leader) {
				data.orJointlyer = '1';
			}
			if (this.motion.wyhdz) {
				data.leaderWyhdz = this.motion.wyhdz;
			}
			if (this.motion.dept) {
				data.zbHandlerId = this.motion.dept;
			}
			if (this.motion.deptXb) {
				data.xbHandlerId = this.motion.deptXb;
			}
			this.$request(url, data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.proposalList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.proposalList = this.proposalList.concat(res.data.records);
				this.loaded = true;
				if (this.proposalList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		loadDept() {
			this.$request('/system/sys-dept/depts', {
				clientCode: this.clientCode
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this.deptList = [];
				this.deptXbList = [];
				res.data.forEach((item) => {
					this.deptList.push({
						value: item.id,
						label: item.deptName
					});
					this.deptXbList.push({
						value: item.id,
						label: item.deptName
					});
				});
			});
		},
		timeChange({ date, dt }, idx) {
			this[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.typeFun(this.tabsList[index].value);
		},
		jcConfirm(e) {
			this.jieci = e[0].label + e[1].label;
			this.motion.jieId = e[0].value;
			this.motion.ciId = e[1].value;
		},
		deptConfirm(e) {
			this.dept = e[0].label;
			this.motion.dept = e[0].value;
		},
		deptXbConfirm(e) {
			this.deptXb = e[0].label;
			this.motion.deptXb = e[0].value;
		},
		wyhdzConfirm(e) {
			this.wyhdz = e[0].label;
			this.motion.wyhdz = e[0].value;
		},
		typeConfirm(e) {
			this.type = e[0].label;
			this.motion.type = e[0].value;
		},
		filter() {
			this.show = false;
			this.motion.dateRangeSea = this.dateRangeSea;
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.motion.title = '';
			this.motion.leaderName = '';
			this.jieci = this.userInfo.jieCi.pName + this.userInfo.jieCi.name;
			this.motion.jieId = this.userInfo.jieCi.pid;
			this.motion.ciId = this.userInfo.jieCi.id;
			this.type = '';
			this.motion.type = '';
			this.dept = '';
			this.motion.dept = '';
			this.deptXb = '';
			this.motion.deptXb = '';
			this.wyhdz = '';
			this.motion.wyhdz = '';
			this.dateRangeSea = '';
			this.motion.dateRangeSea = '';
			this.loadData('refresh');
		},
		search(e) {
			this.motion.title = e;
			this.loadData('refresh');
		},
		typeFun(e) {
			this.motion.nodeStatus = e;
			this.loadData('refresh');
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				this[list] = [];
				res.data.forEach((item) => {
					this[list].push({
						label: item.dictName,
						value: item.dictValue
					});
				});
			});
		},
		jieciData() {
			let data = {
				clientCode: this.clientCode,
				jieCi: {
					pid: ''
				}
			};
			this.$request('/jie-ci/list', data).then((res) => {
				this.jcList = [];
				let jieArr = [];
				let ciArr = [];
				res.data.forEach((item) => {
					data.jieCi.pid = item.id;
					this.$request('/jie-ci/list', data).then((res) => {
						ciArr = [];
						res.data.forEach((key) => {
							ciArr.push({
								value: key.id,
								label: key.name
							});
						});
						this.jcList.push({
							value: item.id,
							label: item.name,
							children: ciArr
						});
					});
				});
			});
		},
		navTo(item) {
			uni.navigateTo({
				url: '/pagesB/proposal/detail?id=' + item.id
			});
		}
	}
};
</script>

<style lang="scss">
.list-box {
	padding: 0 30rpx;
	background-color: #fff;
}

.filter-box {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
